package com.hzj.springcloud.controller;

import com.hzj.springcloud.pojo.Dept;
import com.hzj.springcloud.service.DeptService;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RestController//表示所有Controller接口返回字符串
public class DeptController {
    @Autowired
    DeptService ds;//注入service


    @GetMapping("/dept/get/{id}")
    @HystrixCommand(fallbackMethod = "showHystrix")//定义备选方法，备选方法入参和返回值必须和主方法一致，当抛出异常时就会走备选方法
    //根据id查询
    public Dept queryById(@PathVariable("id") int id) {
        Dept dept = ds.queryById(id);
        if (dept == null) {
            throw new RuntimeException("id=>" + id + "不存在");//当抛出异常时就会走备选方法
        }
        return dept;
    }

    //备选方法，备选方法入参和返回值必须和主方法一致
    public Dept showHystrix(int id) {
        return new Dept("进入备用方法,id=>" + id + "不存在");
    }

}
